package main

import "fmt"

func main() {
	sum := 0
	for i := 0; i <= 10; i++ {
		sum += i
	}
	fmt.Println(sum)

	sum = 1
	for ; sum <= 10; {
		sum += sum
	}
	fmt.Println(sum)

	sum = 1
	for sum <= 10 {
		sum += sum
	}
	fmt.Println(sum)

	//sum = 0
	//for {
	//	sum++ // 无限循环
	//}
	//fmt.Println(sum)

	strings := []string {"google", "runoob"}
	for i, s:= range strings {
		fmt.Println(i, s)
	}

	numbers := [6]int {1,2,3,5}
	for i, x:= range numbers {
		fmt.Printf("第 %d 位 x 的值 = %d \n", i, x)
	}

	var i, j int
	for i = 2; i < 100; i++ {
		for j = 2; j <= (i / j); j++ {
			if (i % j == 0) {
				break // 如果发现因子，则不是素数
			}
		}
		if (j > (i / j)) {
			fmt.Printf("%d 是素数\n", i)
		}
	}

	// break
	var a int = 10
	for a < 20 {
		fmt.Printf("a 的值为：%d\n", a)
		a++
		if a > 15 {
			break
		}
	}
	// 不使用标记
	fmt.Println("---- break ----")
	for i := 1; i <= 3; i++ {
		fmt.Printf("i: %d\n", i)
		for i2 := 11; i2 <= 13; i2++ {
			fmt.Printf("i2: %d\n", i2)
			break
		}
	}
	// 使用标记
	fmt.Println("------ break label ------")
	re:
		for i := 1; i <= 3; i++ {
			fmt.Printf("i: %d\n", i)
			for i2 := 11; i2 < 13; i2++ {
				fmt.Printf("i2: %d\n", i2)
				break re
			}
		}

	// continue
	a = 10
	for a < 20 {
		if a == 15 {
			a = a + 1
			continue;
		}
		fmt.Printf("a 的值为：%d\n", a)
		a++
	}
	// 不使用标记
	fmt.Println("---- continue ----")
	for i := 1; i <= 3; i++ {
		fmt.Printf("i: %d\n", i)
		for i2 := 11; i2 <= 13; i2++ {
			fmt.Printf("i2: %d\n", i2)
			continue
		}
	}
	// 使用标记
	fmt.Println("------ continue label ------")
	ree:
		for i := 1; i <= 3; i++ {
			fmt.Printf("i: %d\n", i)
			for i2 := 11; i2 < 13; i2++ {
				fmt.Printf("i2: %d\n", i2)
				continue ree
			}
		}

	// goto
	a = 10
	LOOP: for a < 20 {
		if a == 15 {
			a = a + 1
			goto LOOP
		}
		fmt.Printf("a 的值为：%d\n", a)
		a++
	}
}
